package com.szh.es.testcase;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;


@Slf4j
public class ESDocmentTestCase {

    public static void main(String[] args) throws IOException {
        // 创建客户端
        RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200)));

        // 新增文档
        // IndexRequest indexRequest = new IndexRequest("user");
        // indexRequest.id("1001");
        //
        // // 准备文档
        // User user = new User();
        // user.setName("张三");
        // user.setAge(22);
        // user.setSex("男");
        //
        // String userJson = JSONObject.toJSONString(user);
        // indexRequest.source(userJson, XContentType.JSON);
        //
        // IndexResponse indexResponse = esClient.index(indexRequest, RequestOptions.DEFAULT);
        //
        // log.error("getResult:==========>:{}",indexResponse.getResult());

        // 批量新增文档

        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(new IndexRequest("user").id("2001").source(XContentType.JSON,"name","张三","age","40","sex","男"));
        bulkRequest.add(new IndexRequest("user").id("2002").source(XContentType.JSON,"name","222","age","10","sex","女"));
        bulkRequest.add(new IndexRequest("user").id("2003").source(XContentType.JSON,"name","33333","age","20","sex","男"));
        bulkRequest.add(new IndexRequest("user").id("2004").source(XContentType.JSON,"name","111","age","30","sex","男"));
        bulkRequest.add(new IndexRequest("user").id("2005").source(XContentType.JSON,"name","2222","age","31","sex","女"));
        BulkResponse bulkResponse = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        log.error("getResult:==========>:{}",bulkResponse.getTook());

        // 更新文档（全量更新，局部更新）

        // UpdateRequest updateRequest = new UpdateRequest("user", "1001");
        // updateRequest.doc("sex","dddddd");
        // UpdateResponse updateResponse = esClient.update(updateRequest, RequestOptions.DEFAULT);
        // log.error("getResult:==========>:{}",updateResponse.getResult());

        // 根据_id查询文档
        // GetRequest getRequest = new GetRequest("user", "1001");
        // GetResponse getResponse = esClient.get(getRequest, RequestOptions.DEFAULT);
        // log.error("getResult:==========>:{}",getResponse.getSource());


        // 根据_id 删除数据
        // DeleteRequest deleteRequest = new DeleteRequest("user", "1001");
        // DeleteResponse deleteResponse = esClient.delete(deleteRequest, RequestOptions.DEFAULT);
        // log.error("getResult:==========>:{}",deleteResponse.getResult());

        // 批量删除（和批量新增类似）




        esClient.close();
    }
}
